package cn.shop.crontab;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author : 李一涵
 * @date : Created in 2022/5/28 10:33
 * @description :
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.mq")
@ConditionalOnExpression("'${spring.profiles.active:dev}' == 'online'")
public class AliyunMQConfig {

    /**
     * 阿里云 oss 公钥
     */
    private String accessKeyId;

    /**
     * 阿里云 oss 私钥
     */
    private String accessKeySecret;

    /**
     * 实例TCP协议接入地址（内网）
     */
    private String nameSrvAddr;

    public Properties getMqProperties() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, this.accessKeyId);
        properties.setProperty(PropertyKeyConst.SecretKey, this.accessKeySecret);
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, this.nameSrvAddr);
        // 设置发送超时时间，单位毫秒
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
        return properties;
    }

    /**
     * 获取消息队列配置
     */
    @Data
    @ToString
    public static class TopicProperties {
        private String topic;
        private String groupId;
        private String tag;
    }

    /**
     * 订单
     */
    @Bean(name = "orderTopicProperties")
    @ConfigurationProperties(prefix = "aliyun.mq.order")
    public TopicProperties orderTopicProperties() {
        return new TopicProperties();
    }

    /**
     * 删除分区
     */
    @Bean(name = "removePartitionTopicProperties")
    @ConfigurationProperties(prefix = "aliyun.mq.remove")
    public TopicProperties removePartitionTopicProperties() {
        return new TopicProperties();
    }

}
